# -*- coding:utf-8 -*-
from Tkinter import *
import GlobalVar
import MyFunc

class View:
    def __init__(self):
        self.root = self.var = self.lb = None
        self.main_win()

    def main_win(self):
        self.root = Tk()
        self.root.title("键盘分享控制")
        self.root.geometry('300x300')
        self.root.resizable(width=False, height=False)
        Label(self.root, width=280, text="点击ip地址切换", bg="#EAEAEA", font=("微软雅黑", 15), bd=5, relief=RIDGE).pack()
        # self.new_list(['192.168.131.1', '192.168.131.3', '192.168.131.4'])
        self.new_list([])

    def run(self):
        self.root.mainloop()

    def new_list(self, val_list):
        self.var = StringVar()

        self.lb = Listbox(self.root, font=("微软雅黑", 15), width=280, listvariable=self.var, background='#FFFFE0', bd=5,
                          relief=RIDGE, selectmode=SINGLE, takefocus=0, exportselection=0, activestyle='none')
        self.var.set(tuple(val_list))
        self.lb.bind('<ButtonRelease-1>', self.switch_ip)
        self.lb.pack(fill=Y)

    def reset_list_box(self, val_list):
        """
        重新设置列表
        :param val_list: 
        :return: 
        """
        current_select = False
        if self.lb.curselection():
            current_select = self.lb.get(self.lb.curselection())
        self.lb.selection_clear(0, END)
        self.var.set(tuple(val_list))
        if current_select not in val_list:
            current_select = MyFunc.getIp(False)
        self.lb.selection_set(val_list.index(current_select))

    def switch_ip(self, e=False):
        if self.lb.curselection():
            GlobalVar.set_switch(self.lb.get(self.lb.curselection()))


if __name__ == '__main__':
    view = View()
    view.run()
